﻿using System;
using System.Collections.Generic;
using System.Linq;
using Luna.Data.Common;
using NUnit.Framework;

namespace Luna.Data.Test.Common
{
    public class GeolocalisationSearchTest : EntityTest<ISearchProvider, IGeolocalisationData, Guid>
    {
        public GeolocalisationSearchTest(string contextName)
            : base(contextName)
        {
        }

        public override Action<Guid> Delete
        {
            get { return Service.GeolocalisationDelete; }
        }

        public override Predicate<IGeolocalisationData> DeletePredicate
        {
            get { return x => x.DisplayName.StartsWith(DataManager.Generator.SeedString); }
        }

        public override Func<Guid, IGeolocalisationData> Get
        {
            get { return Service.GeolocalisationGet; }
        }

        public override Func<IEnumerable<IGeolocalisationData>> GetAll
        {
            get { return Service.GeolocalisationGetAll; }
        }

        public override Func<IGeolocalisationData, Guid> KeyGetter
        {
            get { return x => x.PK; }
        }

        public override Action<IGeolocalisationData> Save
        {
            get { return Service.GeolocalisationSave; }
        }

        protected override IGeolocalisationData Create()
        {
            var entity = Context.Get<IGeolocalisationData>();
            DataManager.Load(entity);
            return entity;
        }

        protected override bool AreSame(IGeolocalisationData data1, IGeolocalisationData data2)
        {
            return DataManager.AreSame(data1, data2);
        }

        [Test]
        public void TestSearch()
        {
            var entity = Create();
            entity.Latitude = 0;
            entity.Longitude = 0;

            var entity2 = Create();
            entity2.Latitude = 0.01;
            entity2.Longitude = 0.01;

            Save(entity);
            Save(entity2);

            var result1 = Service.GeolocalizationSearch(entity, 2000, 2);
            Expect(result1.Count(), Is.EqualTo(2));

            var result2 = Service.GeolocalizationSearch(entity, 2000, 1);
            Expect(result2.Count(), Is.EqualTo(1));

            var result3 = Service.GeolocalizationSearch(entity, 1, 2);
            Expect(result3.Count(), Is.EqualTo(1));
        }
    }
}